﻿#ifndef __SS_DISPATCH_H__
#define __SS_DISPATCH_H__
#include "../ssBaseDefine.h"
#include "../container/stdHashmap.h"
#include "./ssNetMsgHead.h"
#include "./ssMsgIdDef.h"
#include "./ssInvokeInfo.h"

namespace StarSeeker
{

#define SS_EVENT_MAP(evnId, func)		m_func_map[evnId] = (fcEventPtr)(&func);
#define SS_NET_MAP(evnId, func)		m_net_func_map[evnId] = (fcNetEventPtr)(&func);

class ssDispatch
{
public:
	ssDispatch() {}
	virtual ~ssDispatch() {}

	inline bool Dispatch(u32 msgId, u32 subId, ulint param1, ulint param2)
	{
		if (msgId == SS_MSG_RECRIVE)//网络消息
		{
			net_map_it it = m_net_func_map.find(subId);
			if (it != m_net_func_map.end())
			{
				ssNetMsgHead* head = (ssNetMsgHead*)param2;
				char* buf = (char*)param2;
				return (this->*(it->second))(subId, (u32)param1, head, buf + MSG_HEADER_LEN, head->m_dwLen - MSG_HEADER_LEN);
			}
		}
		else
		{
			map_it it = m_func_map.find(subId);
			if (it != m_func_map.end())
			{
				return (this->*(it->second))(subId, param1, param2);
			}
		}
		return true;
	}

protected:
	typedef bool(ssDispatch::*fcEventPtr)(u32 subId, ulint param1, ulint param2);
	typedef HASH_MAP<u32, fcEventPtr>::iterator map_it;

	typedef bool(ssDispatch::*fcNetEventPtr)(u32 msgId, u32 connectId, ssNetMsgHead* head, const char* data, u32 len);
	typedef HASH_MAP<u32, fcNetEventPtr>::iterator net_map_it;

	HASH_MAP<u32, fcEventPtr>		m_func_map;
	HASH_MAP<u32, fcNetEventPtr>	m_net_func_map;
};

}
#endif
